home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ML_BME1.ZIP / _LIB_ / FCACHE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-23  |  1.7 KB  |  88 lines

  1. Unit FCache;
  2.  
  3. {
  4.    Cache work with files. (for better speed on accesing disk)
  5.    by Maple Leaf, 1996
  6. }
  7.  
  8. Interface
  9.  
  10. Const
  11.   MaxCache              = 4*1024;   { 4K }
  12.   CacheError  : byte    = 0;
  13.   CacheEOF    : Boolean = False;
  14.  
  15. (*
  16.   'CacheError' values:
  17.  
  18.      0  = No error
  19.      1  = Write error/Disk full
  20.      2  = Is End-Of-File
  21. *)
  22.  
  23. Var
  24.   OutBuffIndex,InBuffIndex : Word;
  25.   MaxCacheRead             : Word;
  26.   OBuff, IBuff             : Array [1..MaxCache] of byte;
  27.  
  28. { Write cache }
  29. Procedure FlushBuffer(var f:file);
  30. Function  WriteByte(var f:file; b:byte):Boolean;
  31. { Read cache }
  32. Procedure ResetBuffer;
  33. Function  ReadByte(var f:file) : Byte;
  34.  
  35. Implementation
  36.  
  37. Procedure FlushBuffer(var f:file);
  38. var k:word;
  39. begin {$i-}
  40.   CacheError:=0;
  41.   CacheEOF:=False;
  42.   if (OutBuffIndex>0) and (OutBuffIndex<=MaxCache) then begin
  43.     BlockWrite(f,OBuff,OutBuffIndex,k);
  44.     if k<>OutBuffIndex then CacheError:=1;
  45.   end;
  46.   OutBuffIndex:=0;
  47. end;
  48.  
  49. Function WriteByte(var f:file; b:byte):Boolean;
  50. begin {$i-}
  51.   CacheError:=0;
  52.   CacheEOF:=False;
  53.   Inc(OutBuffIndex);
  54.   if OutBuffIndex>MaxCache then begin
  55.     dec(OutBuffIndex);
  56.     FlushBuffer(f);
  57.     OutBuffIndex:=1;
  58.   end;
  59.   OBuff[OutBuffIndex]:=b;
  60.   WriteByte:=CacheError=0;
  61. end;
  62.  
  63. Procedure ResetBuffer;
  64. begin
  65.   InBuffIndex:=MaxCache+1;
  66.   MaxCacheRead:=0;
  67. end;
  68.  
  69. Function ReadByte(var f:file):Byte;
  70. begin {$i-}
  71.   CacheEOF:=False;
  72.   CacheError:=0;
  73.   Inc(InBuffIndex);
  74.   if InBuffIndex>MaxCacheRead then begin
  75.     InBuffIndex:=1;
  76.     BlockRead(f,IBuff,MaxCache,MaxCacheRead);
  77.     if MaxCacheRead=0 then begin
  78.       CacheEOF:=True;
  79.       CacheError:=2;
  80.       InBuffIndex:=0;
  81.     end;
  82.   end;
  83.   ReadByte:=IBuff [ InBuffIndex ] ;
  84. end;
  85.  
  86. begin
  87. end.
  88.